/**
 * Created by Administrator on 2017/7/31.
 */
define(function (require, exports) {
	var pubjs = require('pubjs');
	var $ = require('jquery');
	var grid = require('@base/highGrid');
	var util = require('util');
	var TYPE = {
		"FORBID": -2,
		"SYSTEM": -1,
		"CHANNEL": 1,
		"SPOT": 2,
		"DEAL": 3,
	};
	var Base = grid.base.extend({
		init: function (config) {
			config = pubjs.conf(config, {
				url: 'premium/list',
				urlAudit: 'premium/list',
				urlRemove: 'premium/list',
				reqType: 'websocket',
				hasTab: false,
				hasAmount: false,
				hasSearch: false,
				hasSwitch: false,
				hasExport: false,
				hasMenu: false,
				hasRefresh: true,
				hasSubGrid: false,
				hasSelect: false,
				hasPager: true,
				auto_load: true,
				cols: [
					{name: '_id', text: LANG("ID"), width: 60, align: 'center'},
					{name: "Name", text: LANG("测试规则名称"), align: 'left', width: 250, },
					{name: "Value", text: LANG("溢价比例"), align: 'center', width: 150, render: 'renderValue'},
				],
				metrics: [
					{name: "idsObject", text: LANG("目标渠道"), align: 'left' , render: 'renderName' },
					{name: "userObject", text: LANG("生效范围"), align: 'left',render: 'renderUserName'},
					{type: "_id", text:LANG("快速操作"), render: 'renderEdit', align: 'center',width:100},
				],
				param: {
					order: 'CreateTime|-1'
				},
				gridName: 'aptitude'
			});
			this.Super('init', arguments);
		},
		afterBuild: function () {
			this.Super('afterBuild', arguments);
		},
		renderName: function (i, val, data ) {
			var str = [];
			var len = 5; //显示len个
			if(val) {
				for (var i = 0, j = val.length; i < j && i < len; i++) {
					str.push(val[i].Name);
				}
				if (val.length > len) {
					len = "...";
				} else {
					len = '';
				}
				return str.join('、') + len;
			}
		}
		,renderUserName: function (i, val, data ) {
			var str = [];
			var len = 5; //显示len个
			if(val){
				for(var i =0,j=val.length;i<j && i<len;i++){
					str.push(val[i].UserName);
				}
				if(val.length>len){
					len = "...";
				}else{
					len = '';
				}
				return str.join('、') + len;
			}else{
				return "所有子账号";
			}
		}
		,renderValue: function (i, val, data) {
			return val + "%";
		}
		,renderEdit: function (i, val, data) {
			var html = [];
			var self = this;
			html.push($('<a href="javascript:void(0)" title="编辑"/>').text(LANG('编辑')).click(function () {
				self.fire('showPopList',data);
			}));
			html.push(" | ");
			if(data.IsDeleted){
				html.push($('<a href="javascript:void(0)" title="还原"/>').text(LANG('还原')).click(function () {
					self.fire('recoverList', data);
				}));
			}else{
				html.push($('<a href="javascript:void(0)" title="归档"/>').text(LANG('归档')).click(function () {
					self.fire('removeList', data);
				}));
			}
			return html;
		}
	});
	exports.base = Base;
	var Spot = grid.base.extend({
		init: function (config) {
			config = pubjs.conf(config, {
				url: 'premium/list',
				urlAudit: 'premium/list',
				urlRemove: 'premium/list',
				reqType: 'websocket',
				hasTab: false,
				hasAmount: false,
				hasSearch: false,
				hasSwitch: false,
				hasExport: false,
				hasMenu: false,
				hasRefresh: true,
				hasSubGrid: false,
				hasSelect: false,
				hasPager: true,
				auto_load: true,
				cols: [
					{name: '_id', text: LANG("ID"), width: 60, align: 'center'},
					{name: "Name", text: LANG("测试规则名称"), align: 'left', width: 250, },
					{name: "Value", text: LANG("溢价比例"), align: 'center', width: 150,render: 'renderValue' },
				],
				metrics: [
					{name: "idsObject", text: LANG("目标广告位"), align: 'left',render: 'renderName' },
					{name: "userObject", text: LANG("生效范围"), align: 'left',render: 'renderUserName'}
					,{type: "_id", text:LANG("快速操作"), render: 'renderEdit', align: 'center',width:100},
				],
				param: {
					order: 'CreateTime|-1'
				},
				gridName: 'aptitude'
			});
			this.Super('init', arguments);
		},
		afterBuild: function () {
			this.Super('afterBuild', arguments);
		},
		renderName: function (i, val, data ) {
			var str = [];
			var len = 5; //显示len个
			if(val) {
				for (var i = 0, j = val.length; i < j && i < len; i++) {
					str.push(val[i].Name);
				}
				if (val.length > len) {
					len = "...";
				} else {
					len = '';
				}
				return str.join('、') + len;
			}
		}
		,renderUserName: function (i, val, data ) {
			var str = [];
			var len = 5; //显示len个
			if(val){
				for(var i =0,j=val.length;i<j && i<len;i++){
					str.push(val[i].UserName);
				}
				if(val.length>len){
					len = "...";
				}else{
					len = '';
				}
				return str.join('、') + len;
			}else{
				return "所有子账号";
			}
		}
		,renderValue: function (i, val, data) {
			return val + "%";
		}
		,renderEdit: function (i, val, data) {
			var html = [];
			var self = this;
			html.push($('<a href="javascript:void(0)" title="编辑"/>').text(LANG('编辑')).click(function () {
				self.fire('showPopList',data);
			}));
			html.push(" | ");
			if(data.IsDeleted){
				html.push($('<a href="javascript:void(0)" title="还原"/>').text(LANG('还原')).click(function () {
					self.fire('recoverList', data);
				}));
			}else{
				html.push($('<a href="javascript:void(0)" title="归档"/>').text(LANG('归档')).click(function () {
					self.fire('removeList', data);
				}));
			}
			return html;
		}
	});
	exports.spot = Spot;
	var Deal = grid.base.extend({
		init: function (config) {
			config = pubjs.conf(config, {
				url: 'premium/list',
				urlAudit: 'premium/list',
				urlRemove: 'premium/list',
				reqType: 'websocket',
				hasTab: false,
				hasAmount: false,
				hasSwitch: false,
				hasExport: false,
				hasRefresh: true,
				hasSubGrid: false,
				hasSelect: false,
				hasPager: true,
				auto_load: true,
				param: {Type:TYPE.DEAL,order: 'CreateTime|-1'},
				hasSearch: true,
				hasFilter: true,
				hasMenu: true,
				cols: [
					{name: '_id', text: LANG("ID"), width: 60, align: 'center'},
					{name: "Name", text: LANG("测试规则名称"), align: 'left', width: 250, },
					{name: "Value", text: LANG("溢价比例"), align: 'center', width: 90, render: 'renderValue'},
				],
				metrics: [
					{name: "idsObject", text: LANG("目标订单"), align: 'left',render: 'renderName' },
					{name: "userObject", text: LANG("生效范围"), align: 'left',render: 'renderUserName'}
					,{type: "_id", text:LANG("快速操作"), render: 'renderEdit', align: 'center',width:100},
				],
				gridName: 'aptitude'
			});
			this.Super('init', arguments);
		},
		afterBuild: function () {
			this.Super('afterBuild', arguments);
		},
		renderName: function (i, val, data ) {
			var str = [];
			var len = 2; //显示len个
			if(val) {
				for (var i = 0, j = val.length; i < j && i < len; i++) {
					str.push(val[i].Name);
				}
				if (val.length > len) {
					len = "...";
				} else {
					len = '';
				}
				return str.join('、') + len;
			}
		}
		,renderUserName: function (i, val, data ) {
			var str = [];
			var len = 3; //显示len个
			if(val){
				for(var i =0,j=val.length;i<j && i<len;i++){
					str.push(val[i].UserName);
				}
				if(val.length>len){
					len = "...";
				}else{
					len = '';
				}
				return str.join('、') + len;
			}else{
				return "所有子账号";
			}
		}
		,renderValue: function (i, val, data) {
			return val + "%";
		}
		,renderEdit: function (i, val, data) {
			var html = [];
			var self = this;
			html.push($('<a href="javascript:void(0)" title="编辑"/>').text(LANG('编辑')).click(function () {
				self.fire('showPopList',data);
			}));
			html.push(" | ");
			if(data.IsDeleted){
				html.push($('<a href="javascript:void(0)" title="还原"/>').text(LANG('还原')).click(function () {
					self.fire('recoverList', data);
				}));
			}else{
				html.push($('<a href="javascript:void(0)" title="归档"/>').text(LANG('归档')).click(function () {
					self.fire('removeList', data);
				}));
			}
			return html;
		}
	});
	exports.deal = Deal;
});